package com.nursery.kindergarten.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.nursery.kindergarten.entity.KindergartenInfo;
import com.nursery.kindergarten.entity.KindergartenInfoExample;
import com.nursery.kindergarten.service.KindergartenInfoService;

@Controller
@RequestMapping("kindergarten")
public class KindergartenInfoController {

	@Autowired
	KindergartenInfoService kindergartenInfoService;

	@ResponseBody
	@RequestMapping(value = "search", produces = "application/json; charset=utf-8")
	public String search(@RequestParam Map<String, String> map, String callback) {
		KindergartenInfoExample example = new KindergartenInfoExample();
		KindergartenInfoExample.Criteria criteria = example.createCriteria();
		// id
		if (StringUtils.isNotBlank(map.get("id"))) {
			criteria.andIdEqualTo(Integer.parseInt(map.get("id")));
		}
		// 幼儿园名称
		if (StringUtils.isNotBlank(map.get("kindName"))) {
			criteria.andKindNameLike("%" + map.get("kindName") + "%");
		}
		List<KindergartenInfo> list = kindergartenInfoService.selectByExample(example);
		for (KindergartenInfo info : list) {
			info.setSurplusCount(info.getKindUpperLimit() - info.getKindAdmittedCount());
		}
		String json = callback + "(" + JSON.toJSONString(list) + ")";
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "add", produces = "application/json; charset=utf-8")
	public String add(KindergartenInfo kindergartenInfo, String callback) {
		Map<String, String> map = new HashMap<>();
		try {
			kindergartenInfoService.insert(kindergartenInfo);
			map.put("status", "0");
		} catch (Exception e) {
			map.put("status", "1");
		}
		String json = callback + "(" + JSON.toJSONString(map) + ")";
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "selectById", produces = "application/json; charset=utf-8")
	public String selectById(Integer id, String callback) {
		KindergartenInfo kindergartenInfo = kindergartenInfoService.selectByPrimaryKey(id);
		String json = callback + "(" + JSON.toJSONString(kindergartenInfo) + ")";
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "update", produces = "application/json; charset=utf-8")
	public String update(KindergartenInfo kindergartenInfo, String callback) {
		Map<String, String> map = new HashMap<>();
		try {
			kindergartenInfoService.updateByPrimaryKeySelective(kindergartenInfo);
			map.put("status", "0");
		} catch (Exception e) {
			map.put("status", "1");
		}
		String json = callback + "(" + JSON.toJSONString(map) + ")";
		return json;
	}

	@ResponseBody
	@RequestMapping(value = "delete", produces = "application/json; charset=utf-8")
	public String delete(Integer id, String callback) {
		Map<String, String> map = new HashMap<>();
		try {
			kindergartenInfoService.deleteByPrimaryKey(id);
			map.put("status", "0");
		} catch (Exception e) {
			map.put("status", "1");
		}
		String json = callback + "(" + JSON.toJSONString(map) + ")";
		return json;
	}
}
